package com.itbour.web.action.trade;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.trade.ItemComImgDto;
import com.itbour.service.trade.ItemComImgInfoService;
import com.opensymphony.xwork2.ActionContext;

/**
 * 商品通用图片管理Action
 * @author LQ
 */
public class GetItemComImgWebAction extends BaseAction {
	
	private static final long serialVersionUID = 1L;
	private ItemComImgInfoService itemComImgInfoService;
	private ItemComImgDto imComImgDto;
	private HashMap<String, Object> comImgMapResult = new HashMap<String, Object>();
	private List<ItemComImgDto> listResult;
	public List<ItemComImgDto> getListResult() {
		return listResult;
	}

	public void setListResult(List<ItemComImgDto> listResult) {
		this.listResult = listResult;
	}

	private String findDesc;// 通过描述内容查找
	private String findItemTypeL;// 检索条件[商品大分类]
	private String findItemTypeM;// 检索条件[商品中分类]
	private String findItemType;// 检索条件[商品分类]
	
	/**
	 * 应用通用图片到 各个场景 
	 * @throws Exception 
	 */
	public void applicationComImg() throws Exception{
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		if(user!=null&&user.getUser_id()!=null){
			HttpServletRequest request=ServletActionContext.getRequest();
			Integer type_id=Integer.parseInt(request.getParameter("type_id"));
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("type_id", type_id);
			if(parm.get("type_id")!=null){
				itemComImgInfoService.applicationComImg(parm);
				this.writeJSON("ok");
			}else{
				this.writeJSON("no");
			}
		}else{
			this.writeJSON("YOU HAVE NO POWER!!");
		}
	}
	
	/**
	 * 分页获取通用图片信息
	 * @return
	 */
	public String getItemComImgInfo() {
		Map<String, Object> session = ActionContext.getContext().getSession();
		UserInfoDto user = (UserInfoDto) session.get("user");
		if (user != null && user.getUser_id() != null) {
			Map<String, Object> parm = new HashMap<String, Object>();
			HttpServletRequest request = ServletActionContext.getRequest();
			Integer pageNum = Integer.parseInt(request.getParameter("page"));
			Integer pageSize = Integer.parseInt(request.getParameter("rows"));
			if (StringUtils.isNotEmpty(findDesc)) {
				parm.put("desc", findDesc);
			}
			if (StringUtils.isNotEmpty(findItemTypeL)) {
				parm.put("type_id_l", findItemTypeL);
			}
			if (StringUtils.isNotEmpty(findItemTypeM)) {
				parm.put("type_id_m", findItemTypeM);
			}
			if (StringUtils.isNotEmpty(findItemType)) {
				parm.put("type_id", findItemType);
			}
			Page page = itemComImgInfoService.pageListComImgInfo(pageNum,
					pageSize, parm);
			comImgMapResult.put("total", page.getTotalCount());
			comImgMapResult.put("rows", page.getResult());
		}else{
			
		}
		return SUCCESS;
	}

	/*
	 * 添加商品通用图片信息
	 */
	public void addItemComImgInfo() throws Exception {
		if (imComImgDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null&& imComImgDto.getType_id() != null) {
				Map<String, Object> parm = new HashMap<String, Object>();
				parm.put("type_id", imComImgDto.getType_id());
				int count = itemComImgInfoService.countByTypeid(parm);
				if (count > 0) {
					this.writeJSON("exit");
				} else {
					imComImgDto.setUpdate_by(user.getUser_id().toString());
					if (imComImgDto.getId() != null && imComImgDto.getId() > 0) {
						this.updItemComImgInfo();//修改信息
					} else {
						itemComImgInfoService.addItemComImgInfo(imComImgDto);
						this.writeJSON("ok");
					}
				}
			} else {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**
	 * 跟新商品通用图片信息
	 * @throws Exception
	 */
	public void updItemComImgInfo() throws Exception{
		if (imComImgDto != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
			UserInfoDto user = (UserInfoDto) session.get("user");
			if (user != null && user.getUser_id() != null) {
				imComImgDto.setUpdate_by(user.getUser_id().toString());
				itemComImgInfoService.updItemComImgInfo(imComImgDto);
				this.writeJSON("ok");
			} else {
				this.writeJSON("no");
			}
		} else {
			this.writeJSON("no");
		}
	}
	
	/**获取单件通用图片信息用于更新
	 */
	public String getOneItemComImgInfo(){
		HttpServletRequest request=ServletActionContext.getRequest();
		Integer id=Integer.parseInt(request.getParameter("id"));
		if(id!=null){
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("id", id);
			listResult=itemComImgInfoService.selectOneComImgInfo(parm);
		}
		return SUCCESS;
	}
	
	/**
	 * 删除通用图片信息
	 * @throws Exception 
	 */
	public void delComImgInfo() throws Exception{
		HttpServletRequest request=ServletActionContext.getRequest();
		Map<String, Object> session=ActionContext.getContext().getSession();
		UserInfoDto user=(UserInfoDto)session.get("user");
		String ids=request.getParameter("ids");
		if(ids!=null&&user.getUser_id()!=null&&ids.length()>0){
			Map<String, Object> parm=new HashMap<String, Object>();
			parm.put("ids", StringUtils.listToString2(StringUtils.stringToList2(ids)));
			parm.put("update_by", user.getUser_id());
			itemComImgInfoService.delComImgInfo(parm);
			this.writeJSON("ok");
		}
	}
	
	public ItemComImgDto getImComImgDto() {
		return imComImgDto;
	}

	public void setImComImgDto(ItemComImgDto imComImgDto) {
		this.imComImgDto = imComImgDto;
	}

	public ItemComImgInfoService getItemComImgInfoService() {
		return itemComImgInfoService;
	}

	public void setItemComImgInfoService(
			ItemComImgInfoService itemComImgInfoService) {
		this.itemComImgInfoService = itemComImgInfoService;
	}

	public HashMap<String, Object> getComImgMapResult() {
		return comImgMapResult;
	}

	public void setComImgMapResult(HashMap<String, Object> comImgMapResult) {
		this.comImgMapResult = comImgMapResult;
	}

	public String getFindDesc() {
		return findDesc;
	}

	public void setFindDesc(String findDesc) {
		this.findDesc = findDesc;
	}

	public String getFindItemTypeL() {
		return findItemTypeL;
	}

	public void setFindItemTypeL(String findItemTypeL) {
		this.findItemTypeL = findItemTypeL;
	}

	public String getFindItemTypeM() {
		return findItemTypeM;
	}

	public void setFindItemTypeM(String findItemTypeM) {
		this.findItemTypeM = findItemTypeM;
	}

	public String getFindItemType() {
		return findItemType;
	}

	public void setFindItemType(String findItemType) {
		this.findItemType = findItemType;
	}
}
